﻿using System;
using System.Collections.ObjectModel;
using Moq;
using Prompts.Prompting.Model;
using Prompts.Service.PromptService;
using Prompts.Service.ReportExecution;

namespace Test.Prompts.Infrastructure.Fakes
{
    internal class FakeChildPromptLevelServiceClient
    {
        private readonly Mock<IChildPromptLevelServiceClient> _childTreeNodeService;
        private object _stateTemp;

        public FakeChildPromptLevelServiceClient()
        {
            _childTreeNodeService = new Mock<IChildPromptLevelServiceClient>();
        }

        public void SetupGetChildren2(
            string promptName,
            string parameterName,
            string value)
        {
            Func<object, bool> interceptStateFunction = o =>
                {
                    _stateTemp = o;
                    return true;
                };

            _childTreeNodeService
                .Setup(
                    s => s.GetChildrenFor2Async(
                        promptName
                        , parameterName
                        , It.Is<ParameterValue>(v => v.valueField == value)
                        , Match.Create<Object>(o => interceptStateFunction(o))));
        }

        public void SetupGetChildren(
            string promptName,
            string parameterName, 
            ObservableCollection<ParameterValue> parameterValues)
        {
            Func<object, bool> interceptStateFunction = o =>
            {
                _stateTemp = o;
                return true;
            };

            _childTreeNodeService
                .Setup(
                    s => s.GetChildrenForAsync(
                        promptName
                        , parameterName
                        , parameterValues
                        , Match.Create<Object>(o => interceptStateFunction(o))));
        }

        public void RaiseGetChildren2Completed(PromptLevel promptLevel)
        {
            var wrapper = new object[] { promptLevel };
            var args = new GetChildrenFor2CompletedEventArgs(wrapper, null, false, _stateTemp);
            _childTreeNodeService.Raise(s => s.GetChildrenFor2Completed += null, args);
        }

        public void RaiseGetChildrenCompleted(PromptLevel promptLevel)
        {
            var wrapper = new object[] { promptLevel };
            var args = new GetChildrenForCompletedEventArgs(wrapper, null, false, _stateTemp);
            _childTreeNodeService.Raise(s => s.GetChildrenForCompleted += null, args);
        }

        public IChildPromptLevelServiceClient Object
        {
            get { return _childTreeNodeService.Object; }
        }
    }
}
